package org.example.src.service.impl;

import org.example.src.dto.request.StudentProfileRequest;
import org.example.src.dto.response.StudentProfileResponse;
import org.example.src.entity.Resume;
import org.example.src.entity.Student;
import org.example.src.entity.User;
import org.example.src.repository.ResumeRepository;
import org.example.src.repository.StudentRepository;
import org.example.src.repository.UserRepository;
import org.example.src.service.StudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentRepository studentRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private ResumeRepository resumeRepository;

    @Override
    public List<Student> findAllStudents() {
        return studentRepository.findAll();
    }

    @Override
    public Student findStudentById(Long id) {
        return studentRepository.findById(id).orElse(null);
    }

    @Override
    public Student findStudentByStudentId(String studentId) {
        return studentRepository.findByStudentId(studentId).orElse(null);
    }

    @Override
    public Student createStudent(Student student) {
        return studentRepository.save(student);
    }

    @Override
    public Student updateStudent(Long id, Student studentDetails) {
        Student student = studentRepository.findById(id).orElseThrow(() -> new RuntimeException("学生未找到"));
        BeanUtils.copyProperties(studentDetails, student, "id");
        return studentRepository.save(student);
    }

    @Override
    public void deleteStudent(Long id) {
        studentRepository.deleteById(id);
    }

    @Override
    public StudentProfileResponse updateStudentProfile(Long id, StudentProfileRequest request) {
        Student student = studentRepository.findById(id).orElseThrow(() -> new RuntimeException("学生未找到"));

        student.setStudentId(request.getStudentId());
        student.setFullName(request.getFullName());
        student.setMajor(request.getMajor());
        student.setGrade(request.getGrade());
        student.setClassName(request.getClassName());
        student.setResume(request.getResume());
        student.setProfilePicture(request.getProfilePicture());

        Student updatedStudent = studentRepository.save(student);

        // 更新用户信息
        User user = userRepository.findById(id).orElse(null);
        if (user != null) {
            user.setEmail(updatedStudent.getEmail());
            userRepository.save(user);
        }

        StudentProfileResponse response = new StudentProfileResponse();
        BeanUtils.copyProperties(updatedStudent, response);
        if (user != null) {
            response.setUsername(user.getUsername());
            response.setEmail(user.getEmail());
        }

        return response;
    }

    @Override
    public Resume createResume(Long studentId, Resume resume) {
        Student student = studentRepository.findById(studentId)
                .orElseThrow(() -> new RuntimeException("学生未找到"));

        resume.setStudent(student);
        resume.setCreatedAt(LocalDateTime.now());
        resume.setUpdatedAt(LocalDateTime.now());

        return resumeRepository.save(resume);
    }

    @Override
    public List<Resume> getResumesByStudentId(Long studentId) {
        Student student = studentRepository.findById(studentId)
                .orElseThrow(() -> new RuntimeException("学生未找到"));

        return resumeRepository.findByStudent(student);
    }

    @Override
    public Resume updateResume(Long resumeId, Resume resume) {
        Resume existingResume = resumeRepository.findById(resumeId)
                .orElseThrow(() -> new RuntimeException("简历未找到"));

        existingResume.setTitle(resume.getTitle());
        existingResume.setContent(resume.getContent());
        existingResume.setEducation(resume.getEducation());
        existingResume.setSkills(resume.getSkills());
        existingResume.setExperience(resume.getExperience());
        existingResume.setUpdatedAt(LocalDateTime.now());

        return resumeRepository.save(existingResume);
    }

    @Override
    public void deleteResume(Long resumeId) {
        Resume resume = resumeRepository.findById(resumeId)
                .orElseThrow(() -> new RuntimeException("简历未找到"));

        resumeRepository.delete(resume);
    }
}
